package com.songlanyun.modules.rights.entity;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.IOException;
import java.math.BigDecimal;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.RightsOrderConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import com.songlanyun.modules.rights.service.RightsChildOrderService;
import com.songlanyun.modules.rights.service.RightsOrderOperateRecordService;
import com.songlanyun.modules.rights.service.RightsOrderShipRecordService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 销售系统中的维权单
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 13:45:42
 */
@Data
@TableName("yxt_sale_rights_order")
public class RightsOrder extends RootEntity<Long,RightsOrder> implements Serializable {
	private static final long serialVersionUID = 1L;


	{
		rightsRefundStatus = RightsOrderConstant.RightsRefundStatus.WAIT;
		rightsOperateStatus = RightsOrderConstant.RightsOperateStatus.APPLY;
		rightsOperatePerson = RightsOrderConstant.RightsOperatePerson.USER;
		valid = true;
		isAllRights = true;
		payRefundCode = "";
		thirdRefundCode = "";
		payRefundId = 0L;
		lastAgreeTime=0L;
		lastShipTime=0L;
		lastReceiptTime=0L;
		receiver="";
		receiverMobile="";
		refundPoint=BigDecimal.ZERO;
		thirdRightsId=0L;
		third = false;

	}


	/**
	 *
	 */
	@TableId
	private Long id;

	/**
	 * 维权单号
	 */
	@NotBlank(message = "维权单号不能为空")
	@Length(min = 10, max = 50, message = "维权单号长度在10-50个字")
	private String rightsCode;
	/**
	 * 所属商家id( 可操作该维权的商家)
	 */
	@NotNull(message = "所属商家id不能为空")
	private Long shopId;
	/**
	 * 所属商家( 可操作该维权的商家)
	 */
	@NotBlank(message = "所属商家不能为空")
	private String shopName;
	/**
	 * 所属订单id
	 */
	@NotNull(message = "所属订单id不能为空")
	private Long orderId;

	@TableField(exist = false)
	private Order order;

	/**
	 * 支付退款单id
	 */
	private Long payRefundId;

	/**
	 * 支付退款单编号
	 */
	private String payRefundCode;

	/**
	 * 商品分区特征(0,普通商品,1服务体系礼包,2新人福利区,3公益申领)
	 */
	private GoodsConstant.AreaFeature areaFeature = GoodsConstant.AreaFeature.GENERAL;

	/**
	 * 第三方退款支付编号
	 */
	private String thirdRefundCode;


	public Order loadOrder()
	{
		if(order == null)
		{
			OrderService orderService = (OrderService)SpringContextUtil.getBean("orderService");
			order = orderService.loadByUserIdAndId(getUserId(), getOrderId(), false);
		}
		return order;
	}

	/**
	 * 所属订单编号
	 */
	@NotBlank(message = "所属订单编号不能为空")
	private String orderCode;
	/**
	 * 付方式（0余额，1微信，2支付宝,3银行线下,4现金支付,5积分支付,6积分+余额支付,7积分+微信支付,8积分+支付宝支付）
	 */
	private OrderConstant.PayMode payMode;
	/**
	 * 退款总金额
	 */
	private BigDecimal refundMoney;
	/**
	 * 退款商品数量
	 */
	private Integer refundNum;

	/**
	 * 退还积分
	 */
	private BigDecimal refundPoint;
	/**
	 * 会员id
	 */
	@NotNull(message = "会员id不能为空")
	private Long userId;
	/**
	 * 下单会员手机号
	 */
	@NotBlank(message = "会员手机号不能为空")
	private String mobile;


	/**
	 * 收件人名称
	 */

	private String receiver;
	/**
	 * 收件人手机号
	 */

	private String receiverMobile;


	/**
	 * 维权的货物状态（0未收货，1已收货）
	 */
	private RightsOrderConstant.RightsCargoStatus rightsCargoStatus;
	/**
	 * 维权退款状态（0待退款，1退款成功，2退款失败）
	 */
	private RightsOrderConstant.RightsRefundStatus rightsRefundStatus;
	/**
	 * 维权订单操作状态（0申请，1同意，2买家已发货，3商家已收到退货商品,4商家发起退款支付，5已支付退款,6拒绝，7自动关闭,8撤消
	 */
	private RightsOrderConstant.RightsOperateStatus  rightsOperateStatus;
	/**
	 * 0没有，1仅退款,2退款退货
	 */
	private OrderConstant.RightsType rightsType;

	/**
	 * 维权发起操作人（0会员，1系统,2商家）
	 */
	private RightsOrderConstant.RightsOperatePerson rightsOperatePerson;

	/**
	 * 维权原因
	 */
	@NotBlank(message = "维权原因不能为空")
	private String reason;
	/**
	 * 维权说明
	 */
	private String caption;
	/**
	 * 维权证明图片
	 */
	@JsonIgnore
	private String voucherImagesValue;

	@TableField(exist = false)
	private List<String> voucherImages;

	public void setVoucherImages(List<String> voucherImages) {
		this.voucherImages = voucherImages;
		if(voucherImages.size()==0)
		{
			this.voucherImagesValue = "";
		}
		else
		{
			this.voucherImagesValue = JSONUtil.toJsonStr(this.voucherImages );
		}

	}
	/**
	 * 把字符串映射成对象（多图）
	 */
	public void setVoucherImagesValue(String voucherImagesValue) {
		this.voucherImagesValue = voucherImagesValue;
		if (StringUtils.isEmpty(this.voucherImagesValue)) {
			setVoucherImages(new ArrayList<>());
		} else {
			try {
				setVoucherImages(ClassConvertUtils.stringToClassSnake(voucherImagesValue, new TypeReference<List< String>>() {
				}));
			} catch (IOException e) {
				throw new RRException("图片无法转List");
			}

		}
	}

	/**
	 *  是否有效
	 */
	private Boolean valid;



	/**
	 * 是否是全订单维权（一个主订单只能有一个全维权订单），可以有多个子订单维权（一个子订单也只能有一个维权订单）
	 */
	private Boolean isAllRights;


	@TableField(exist = false)
	private List<RightsChildOrder> rightsChildOrders;

	public List<RightsChildOrder> loadRightsChildOrders()
	{
		if(rightsChildOrders==null)
		{
			RightsChildOrderService childOrderService = (RightsChildOrderService)SpringContextUtil.getBean("rightsChildOrderService");
			rightsChildOrders = childOrderService.loadChildRightsOrders(this.getId());
		}
		return  rightsChildOrders;
	}

	@TableField(exist = false)
	private List<RightsOrderOperateRecord> rightsOrderOperateRecords;

	public List<RightsOrderOperateRecord> loadRightsOrderOperateRecords()
	{
		if(rightsOrderOperateRecords==null)
		{
			RightsOrderOperateRecordService recordService = (RightsOrderOperateRecordService)SpringContextUtil.getBean("rightsOrderOperateRecordService");
			rightsOrderOperateRecords = recordService.loadRightsOrderOperateRecords(this.getId());
		}
		return  rightsOrderOperateRecords;
	}

	@TableField(exist = false)
	private RightsOrderShipRecord rightsOrderShipRecord;
	public RightsOrderShipRecord loadRightsOrderShipRecord()
	{
		if(rightsOrderShipRecord==null)
		{
			RightsOrderShipRecordService recordService = (RightsOrderShipRecordService)SpringContextUtil.getBean("saleRightsOrderShipRecordService");
			rightsOrderShipRecord = recordService.loadByRightsId(this.getId(),false);
		}
		return  rightsOrderShipRecord;
	}


	/**
	 *
	 */
	@TableField(fill= FieldFill.INSERT)
	private Date createTime;
	/**
	 *
	 */
	@TableField(fill= FieldFill.INSERT_UPDATE)
	private Date updateTime;


	/**
	 * 最后同意时间
	 */
	private Long lastAgreeTime;

	/**
	 * 买家最后发货时间（商家同意退货多少天后买家未处理，系统将自动拒绝维权）
	 */
	private Long lastShipTime;

	/**
	 * 商家收货最后收货时间，买家已退货(退货填单)多少天后商家未处理，系统将自动收货。
	 */
	private Long lastReceiptTime;

	/**
	 * 订单类型（1消费订单,2服务订单）
	 */
	private OrderConstant.OrderType orderType;


	/**
	 * 数字链的第三方的维权单号
	 */
	private Long thirdRightsId;

	/**
	 * 是否是第三方的维权单
	 */
	private Boolean third;

	/**
	 * 上报是否成功
	 * @return
	 */
	public Boolean getUploadSuccess()
	{
		return this.getThird() && this.getThirdRightsId()>0;
	}




	@Override
	public Long getIdentity() {
		return getId();
	}

}
